<template>
    <div class="cms-article">
        <div class="cms-article-org">
            <el-table
                    @row-click="handleRowClick"
                    border
                    default-expand-all
                    highlight-current-row
                    :data="catalogTable"
                    row-key="id"
                    height="500"
                    size="mini"
                    style="width: 100%"
                    :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
                <el-table-column
                        prop="name"
                        label="catalog"
                        width="*">
                </el-table-column>
            </el-table>
        </div>
        <div class="cms-article-table">
            <div style="height: 35px">
                <el-form :model="searchForm" :inline="true" size="mini">
                    <el-form-item label="title">
                        <el-input v-model="searchForm.title"></el-input>
                    </el-form-item>
                    <el-form-item label="author">
                        <el-input v-model="searchForm.author"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="listArticle">Search</el-button>
                        <el-button type="primary" @click="listArticle">Refresh</el-button>
                        <el-button type="primary" @click="handleCreate">New</el-button>
                    </el-form-item>
                </el-form>
            </div>
            <el-table
                    border
                    highlight-current-row
                    row-key="ukId"
                    height="500"
                    size="mini"
                    :data="articleTable"
                    style="width: 100%">
                <el-table-column
                        type="selection"
                        width="55">
                </el-table-column>
                <el-table-column
                        prop="title"
                        label="title"
                        width="*">
                </el-table-column>
                <el-table-column
                        prop="author"
                        label="author"
                        width="180">
                </el-table-column>
                <el-table-column
                        prop="status"
                        label="status"
                        width="70">
                    <template slot-scope="scope">
                        <el-switch
                                @change="statusChange($event, scope.row.ukId)"
                                :value="scope.row.status != '1'"
                                active-color="#909399"
                                inactive-color="#67c23a">
                        </el-switch>
                    </template>
                </el-table-column>
                <el-table-column
                        prop="sort"
                        label="sort"
                        width="60">
                </el-table-column>
                <el-table-column
                        label="操作"
                        width="210">
                    <template slot-scope="scope">
                        <el-button
                                size="mini"
                                @click="handlePreview(scope.row.ukId)">预览
                        </el-button>
                        <el-button
                                size="mini"
                                @click="handleEdit(scope.row)">编辑
                        </el-button>
                        <el-button
                                size="mini"
                                type="danger"
                                @click="handleDelete(scope.row.ukId)">删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-dialog fullscreen :visible.sync="showArticleForm" :close-on-click-modal="false" label-width="80px">
                <el-row>
                    <el-col :span="24">
                        <el-form ref="form" :model="articleForm" label-width="80px">
                            <el-form-item label="文章标题">
                                <el-input v-model="articleForm.title" style="width: 100%"></el-input>
                            </el-form-item>
                        </el-form>
                    </el-col>
                    <el-col :span="24">
                        <el-form ref="form" :inline="true" :model="articleForm" label-width="80px">
                            <el-form-item label="文章作者">
                                <el-input v-model="articleForm.author"></el-input>
                            </el-form-item>
                            <el-form-item label="所属栏目">
                                <tree-select
                                        style="width: 200px"
                                        :showCount="true"
                                        :openOnClick="true"
                                        :defaultExpandLevel="3"
                                        noChildrenText="无下级栏目"
                                        v-model="articleForm.catalogId"
                                        :options="catalogTable"
                                        :normalizer="orgTableNormalizer">
                                </tree-select>
                            </el-form-item>
                            <el-form-item label="排序">
                                <el-input v-model="articleForm.sort" type="number" min="0" max="90000"></el-input>
                            </el-form-item>
                            <el-form-item>
                                <el-button type="primary" @click="handleSave">保存</el-button>
                                <el-button @click="showArticleForm = false">取 消</el-button>
                            </el-form-item>
                        </el-form>
                    </el-col>
                </el-row>
                <div ref="editor">
                    <quill-editor
                            ref="myQuillEditor"
                            style="height: 500px"
                            v-model="articleForm.content"
                    />
                </div>
            </el-dialog>
        </div>
    </div>
</template>

<script>
  import TreeSelect from '@riophae/vue-treeselect'
  import '@riophae/vue-treeselect/dist/vue-treeselect.css'

  import {listCatalogTree} from "../../../api/cms/catalog";
  import {deleteArticle, listArticle, saveArticle, selectArticleDetails, updateArticleStatus} from "../../../api/cms/article";

  export default {
    name: "Article",
    components: {TreeSelect},
    data () {
      return {
        showArticleForm: false,
        articleForm: {},
        searchForm: {},
        articleTable: [],
        catalogTable: [],
        orgTableNormalizer (node) {
          if (!node.children.length) {
            delete node.children;
          }
          return {
            id: node.ukId,
            label: node.name
          }
        }
      }
    },
    mounted () {
    },
    methods: {
      handleCreate () {
        this.articleForm = {
          catalogId: this.articleForm.catalogId,
          status: 1
        }
        this.showArticleForm = true
      },
      handleEdit (row) {
        this.articleForm = row
        selectArticleDetails(this.articleForm.ukId).then(result => {
          this.articleForm.content = result.data.articleDetails.content
          this.showArticleForm = true
        }).catch(error => {
          console.log(error)
        })
      },
      handleSave () {
        saveArticle(this.articleForm).then(result => {
          this.listArticle()
          this.showArticleForm = false
        }).catch(error => {
          console.log(error)
        })
      },
      handlePreview(ukId) {
        var url = "https://localhost:8901/api/cms/article/" + ukId
        window.open(url, "_blank")
      },
      handleDelete (ukId) {
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteArticle(ukId).then(result => {
            this.listArticle()
            this.showArticleForm = false
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
          }).catch(error => {
            console.log(error)
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      statusChange (value, articleId) {
        let status = value ? 0 : 1
        updateArticleStatus(articleId, status).then(result => {
          this.listArticle()
        }).catch(error => {
          console.log(error)
        })
      },
      handleRowClick (row) {
        this.articleForm.catalogId = row.ukId
        this.searchForm.catalogId = row.ukId
        this.listArticle()
      },
      listArticle () {
        listArticle(this.searchForm).then(result => {
          this.articleTable = result.data.articles.records
        }).catch(error => {
          console.log(error)
        })
      },
      listCatalogTree () {
        listCatalogTree("0").then(result => {
          this.catalogTable = result.data.catalogTree
        }).catch(error => {
          console.log(error)
        })
      }
    },
    created () {
      this.listCatalogTree()
      this.listArticle()
    }
  }
</script>

<style>
    .cms-article {
        width: 100%;
        height: 100%;
    }

    .cms-article-org {
        width: 300px;
        height: calc(100% - 35px);
        float: left;
    }

    .cms-article-table {
        padding-left: 5px;
        width: calc(100% - 305px);
        height: calc(100% - 35px);
        float: left;
    }

    /* element ui样式重写 */
    .cms-article > .cms-article-org > .el-table,
    .cms-article > .cms-article-table > .el-table {
        height: calc(100% - 5px) !important;
    }

    .cms-article > .cms-article-table > .el-table > .el-table__body-wrapper {
        height: calc(100% - 5px) !important;
    }
</style>
